package com.example.book_system;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/book")
public class BookSystemController {
    @RequestMapping("/login")
    public boolean login(String username, String password) {
        //判断账户和密码是否为空
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        //判断账户密码是否正确
        if("admin".equals(username) && "admin".equals(password)) {
            return true;
        }
        return false;
    }

    @RequestMapping("/display")
    public List<BookInfo> display() {
        return mockData();
    }

    //构造假数据
    public List<BookInfo> mockData() {
        List<BookInfo> list = new ArrayList<>();
        for(int i = 1; i < 16; i++) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(i);
            bookInfo.setName("第" + i + "本");
            bookInfo.setAuthor("张三");
            bookInfo.setCount(i+3);
            bookInfo.setPrice(new BigDecimal(new Random().nextInt(100)));
            bookInfo.setPublish("第" + i + "出版社");
            bookInfo.setStatus(i % 2 == 0 ? 1 : 2);
            if(bookInfo.getStatus() == 1) {
                bookInfo.setStatusCN("已借出");
            }else if(bookInfo.getStatus() == 2) {
                bookInfo.setStatusCN("未借出");
            }
            list.add(bookInfo);
        }
        return list;
    }


}
